import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import axios from 'axios';
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const initApp = async () => {
  try {
    // 从绝对路径加载配置
    const response = await fetch('/config.json'); // 注意这里改为根路径
    const config = await response.json();
    const { ip, backendPort } = config.server;

    // 配置 axios
    axios.defaults.baseURL = `http://${ip}:${backendPort}`;
    console.log('API 地址:', axios.defaults.baseURL);

    // 创建应用实例
    const app = createApp(App);
    // 忽略特定警告
    window.addEventListener('error', (e) => {
      if (/ResizeObserver/.test(e.message)) {
        e.preventDefault();
        console.warn('[Ignored] ResizeObserver error:', e.message); // 可选调试日志
      }
    });
    // 重写ResizeObserver构造函数
    const _ResizeObserver = window.ResizeObserver;
    window.ResizeObserver = class extends _ResizeObserver {
      constructor(callback) {
        const debounced = entries =>
          requestAnimationFrame(() => callback(entries));
        super(debounced);
      }
    };
    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
      app.component(key, component)
    }
    app.config.globalProperties.$axios = axios;

    app.use(router).use(store).use(ElementPlus).mount('#app');
  } catch (error) {
    // 错误处理
    console.error('配置加载失败:', error);
    // 降级逻辑
    axios.defaults.baseURL = 'http://localhost:8090';
    const app = createApp(App);
    app.mount('#app');
  }
}

initApp();
